﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace GameJam
{
	public class ScorePointCtrl : MonoBehaviour 
	{
		[SerializeField]
		int _minCount = 2;
		[SerializeField]
		int _maxCount = 4;

		ScorePoint[] _point;

		List<ScorePoint> _pointList;
		List<ScorePoint> _openedPoints;

        static ScorePointCtrl _instance;
        public static ScorePointCtrl Instance { get { return _instance; } }

		bool _bStarted = false;
		public bool IsStarted 
		{
			get{ return _bStarted;}
			set{ _bStarted = value;}
		}

        private void Awake()
        {
            _instance = this;
        }

        // Use this for initialization
        void Start () 
		{
			_pointList = new List<ScorePoint> ();
			_openedPoints = new List<ScorePoint> ();
			_point = this.GetComponentsInChildren<ScorePoint> (true);

			for (int i = 0; i < _point.Length; ++i) {
				_point [i].IsClosed = true;
				_pointList.Add (_point[i]);
			}
            HideAllPoint();
		}
		
		// Update is called once per frame
		void Update () 
		{
			if (!_bStarted)
            {
                return;
            }

            bool bAllClosed = true;
			for (int i = 0; i < _openedPoints.Count; ++i) {
				bAllClosed &= _openedPoints [i].IsClosed;
			}

			if (bAllClosed) {
				if (_pointList.Count == 0)
                {
                    //Debug.LogWarning("Game Finished");
                    GameCtrl.Instance.GameOver();
                }
                else {
					//Debug.LogWarning ("ScorePointCtrl NextRound");
					NextRound ();
				}
			}
		}
			
		void NextRound()
		{
			int count = Random.Range (_minCount, _maxCount);
			_openedPoints.Clear ();

			if (_pointList.Count > count) {
				for (int i = 0; i < count; ++i) {
					int index = Random.Range (0, _pointList.Count);
					_openedPoints.Add (_pointList [index]);
					_pointList.RemoveAt (index);
				}
			}
            else
            {
				_openedPoints.AddRange (_pointList);
				_pointList.Clear ();
			}

			//maybe these points need a slash effect
			for (int i = 0; i < _openedPoints.Count; ++i) {
				_openedPoints [i].IsClosed = false;
			}
		}

		public void OpenedPoints_Effect()
		{
			for (int i = 0; i < _openedPoints.Count; ++i)
			{
				if (_openedPoints [i].IsClosed = false) {
					//_openedPoints [i].Ring.Play ();
				}
			}
		}

        public void HideAllPoint()
        {
            //_point = this.GetComponentsInChildren<ScorePoint>(true);
            //for (int i = 0; i < _point.Length; ++i)
            //{
            //    _point[i].gameObject.SetActive(false);
            //}
        }

        public void ShowAllPoint()
        {
            //_point = this.GetComponentsInChildren<ScorePoint>(true);
            //for (int i = 0; i < _point.Length; ++i)
            //{
            //    _point[i].gameObject.SetActive(true);
            //}
        }
        public void RestartGame()
        {
            _pointList = new List<ScorePoint>();
            _openedPoints = new List<ScorePoint>();
            _point = this.GetComponentsInChildren<ScorePoint>(true);

            for (int i = 0; i < _point.Length; ++i)
            {
                _point[i].IsClosed = true;
                _point[i].gameObject.SetActive(true);
                _point[i].ResetSpine();
                _pointList.Add(_point[i]);
            }
        }
	}
}
